package zhoug.demo.zui.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Arrays;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;
import zhoug.common.interfaces.OnExpandChangeListener;
import zhoug.common.utils.TimeUtils;
import zhoug.demo.zui.R;
import zhoug.logging.Logger;
import zhoug.zui.ZUIExpandView;
import zhoug.zui.ZUISearchView;
import zhoug.zui.ZUITopView;
import zhoug.zui.ints.OnDismissListener;
import zhoug.zui.ints.OnSearchListener;
import zhoug.zui.ints.OnSelectedListener;
import zhoug.zui.popup.ZUISelectListPopupWindow;

/**
 * 厕所搜索框
 * @Author: zhoug
 * @Date: 2024-07-05
 * @Description:
 */
public class ZUISearchViewActivity  extends AppCompatActivity {
    private static final String TAG = ">>>ZUISearchViewActivity";
    private String mLog="";
    private ZUITopView topview;
    private ZUISearchView searchView;
    private ZUISearchView searchView2;
    private Button btnSetValue1;
    private Button btnSetValue2;
    private TextView tvLog;
    private NestedScrollView scrollView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_search_view);
        findViews();
        searchView.setOnSearchListener(onSearchListener);
        searchView2.setOnSearchListener(onSearchListener);
        btnSetValue1.setOnClickListener(v->{
            searchView.setInput("不触发回调");
            searchView2.setInput("不触发回调");
        });
        btnSetValue2.setOnClickListener(v->{
            searchView.setInput("触发回调",true);
            searchView2.setInput("触发回调",true);
        });

        ZUISelectListPopupWindow<String> dialog=new ZUISelectListPopupWindow<String>(this) {
            @Override
            public String getText(String itemData) {
                return itemData;
            }
        };
        dialog.setData(Arrays.asList("主体","主体类型"));
        dialog.setSelected(0);
        dialog.setOnSelectedListener(new OnSelectedListener() {
            @Override
            public void onSelected(int selectedIndex) {
                String itemData = dialog.getItemData(selectedIndex);
                searchView2.setExpandName(itemData!=null?itemData:"主体");

            }
        });
        dialog.addOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                searchView2.setExpand(false);
            }
        });
        searchView2.setExpandName("主体");

        searchView2.setOnExpandChangeListener(new OnExpandChangeListener<ZUIExpandView>() {
            @Override
            public void onExpand(ZUIExpandView view, boolean expand) {
                if(expand){
                    dialog.showAsDropDown(searchView2);
                }
            }
        });

    }

    private void setLog(String log){
        String currentTime = TimeUtils.getCurrentTime("HH:mm:ss");
        mLog=mLog+currentTime+" : "+log+"\n";
        tvLog.setText(mLog);
        scrollView.fullScroll(View.FOCUS_DOWN);

    }

    private OnSearchListener onSearchListener=new OnSearchListener() {
        @Override
        public void onSearch(String input) {
            Logger.d(TAG, "onSearch:"+input);
            setLog("onSearch:"+input);

        }

        @Override
        public void onClear() {
            Logger.d(TAG, "onClear");
            setLog("onClear");
        }

        @Override
        public void onInputChange(String input) {
            Logger.d(TAG, "onInputChange:"+input);
            setLog("onInputChange:"+input);
        }
    };

    private void findViews() {
        topview = findViewById(R.id.topview);
        searchView = findViewById(R.id.searchView);
        searchView2 = findViewById(R.id.searchView2);
        btnSetValue1 = findViewById(R.id.btn_setValue1);
        btnSetValue2 = findViewById(R.id.btn_setValue2);
        tvLog = findViewById(R.id.tv_log);
        scrollView = findViewById(R.id.scrollView);
    }
}
